package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    @RequestMapping("/findPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return  roleService.findByPage(queryPageBean);

    }


    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){

        try {
            List<Menu> list = roleService.findAllMenu();
            return new Result(true,"查询菜单成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }

    }

    @RequestMapping("/findCheckitemIdsByUsername")
    public Result findAllMenu(String username){
        try {
            System.out.println(username);
            List<Integer> list = roleService.findCheckitemIdsByUsername(username);
            return new Result(true,"查询选中菜单id成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询选中菜单id失败");
        }

    }

    @RequestMapping("/editRoleMenu")
    public Result editRoleMenu(String username,@RequestBody Integer[] checkitemIds){
        try {
            roleService.editRoleMenu(username,checkitemIds);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }


}
